package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.*;

/**
 * @author Jason Whiting
 */
public class Part extends BusinessObject
{

    private String name;
    private String partNumber;
    private double price;
    /** Manufacturer */
    private String manufacturerId;
    private Manufacturer manufacturer;

    /**
     * Creates a new instance of BusinessObject
     * @param id
     */
    public Part(String id)
    {
        super(id);
    }//constructor

    //Quick Access Method
    public void save() throws DataException
    {
        try
        {
            PartDAO.getInstance().save(this);
        } catch (DataException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @return the costOfLabor
     */
    public String getPartNumber()
    {
        return partNumber;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setPartNumber(String partNumber)
    {
        this.partNumber = partNumber;
        setDirty(true);
    }

    /**
     * @return the costOfParts
     */
    public double getPrice()
    {
        return price;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setPrice(double partPrice)
    {
        this.price = partPrice;
        setDirty(true);
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name)
    {
        this.name = name;
        setDirty(true);
    }

    /**
     * @return the manufacturerId
     */
    public String getManufacturerId()
    {
        return manufacturerId;
    }

    /**
     * @param manufacturerId the manufacturerId to set
     */
    public void setManufacturerId(String manufacturerId)
    {
        this.manufacturerId = manufacturerId;
        setDirty(true);
    }

    /**
     * @return the manufacturer from the DAO
     */
    public Manufacturer getManufacturer()
    {
        if (manufacturer == null)
        {
            try
            {
                manufacturer = ManufacturerDAO.getInstance().read(manufacturerId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return manufacturer;
    }

    /**
     * @param manufacturer the manufacturer to set
     */
    public void setManufacturer(Manufacturer manufacturer)
    {
        this.manufacturer = manufacturer;
        this.setManufacturerId(manufacturer.getId());
        setDirty(true);
    }

    @Override
    public String toString()
    {
        return getName();
    }
}
